package me.cloud.ids.jap.service;

import com.fujieid.jap.ids.model.UserInfo;
import com.fujieid.jap.ids.service.IdsClientDetailService;
import com.fujieid.jap.ids.service.IdsUserService;
import me.base.utils.PasswordUtil;
import me.boot.sys.entity.SysUser;
import me.boot.sys.service.SysUserService;
import me.cloud.ids.jap.utils.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lapati5
 * @date 2021/8/4
 */
@Service
public class IdsUserServiceImpl implements IdsUserService {

    @Autowired
    private SysUserService userService;
    @Autowired
    private IdsClientDetailService idsClientDetailService;

    @Override
    public UserInfo loginByUsernameAndPassword(String username, String password, String clientId) {
        UserInfo userInfo = null;
        if (idsClientDetailService.getByClientId(clientId) != null) {
            //使用sha-256对密码进行加密取前20位与数据库中进行验证比对
            SysUser user = userService.getByUsername(username);
            if (user != null && PasswordUtil.validPassword(password, user.getPassword())) {
                userInfo = PojoUtil.userInfoFromEntity(user);
            }
        }
        return userInfo;
    }

    @Override
    public UserInfo getById(String userId) {
        SysUser user = userService.getById(Long.parseLong(userId));
        return PojoUtil.userInfoFromEntity(user);
    }

    @Override
    public UserInfo getByName(String username, String clientId) {
        UserInfo userInfo = null;
        if (idsClientDetailService.getByClientId(clientId) != null) {
            SysUser user = userService.getByUsername(username);
            userInfo = PojoUtil.userInfoFromEntity(user);
        }
        return userInfo;
    }
}
